package org.example.Controller;

import lombok.extern.slf4j.Slf4j;
import org.example.Exception.StudentExistException;
import org.example.Pojo.Clazz;
import org.example.Pojo.Result;
import org.example.Server.ClazzServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
public class ClazzController {

    @Autowired
    private ClazzServer clazzServer;

    @GetMapping("/clazzs")
    public Result getClazzs(String name,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end, Integer page, Integer pageSize) {
        log.info("getClazz start");
        log.info("name:{},begin:{},end:{}", name, begin, end);
        log.info("page:{},pageSize:{}", page, pageSize);
        List<Clazz> list=clazzServer.getClazz(name,begin,end,page,pageSize);
        return Result.success(list);
    }

    @PostMapping("/clazzs")
    public Result addClazz(@RequestBody Clazz clazz) {
        log.info("addClazz start");
        log.info("clazz:{}", clazz);
        clazzServer.addClazz(clazz);
        return Result.success();
    }

    /*
    * 查询班级信息通过ID
    * */
    @GetMapping("/clazzs/{id}")
    public Result getClazzById(@PathVariable Integer id) {
        log.info("getClazzById start");
        log.info("id:{}", id);
        Clazz clazz=clazzServer.getClazzById(id);
        return Result.success(clazz);
    }


    /*
    * 更新员工的信息
    * */
    @PutMapping("/clazzs")
    public Result update(@RequestBody Clazz clazz) {
        log.info("update start");
        log.info("clazz:{}", clazz);
        clazzServer.update(clazz);
        return Result.success();
    }

    /*
    * 根据Id删除班级
    * */
    @DeleteMapping("/clazzs/{id}")
    public Result delete(@PathVariable Integer id) throws StudentExistException {
        log.info("delete start");
        log.info("id:{}", id);
        clazzServer.delete(id);
        return Result.success();
    }

    /*
    * 获取全部的班级
    * */
    @GetMapping("/clazzs/list")
    public Result getAllClazz() {
        log.info("getAllClazz start");
        List<Clazz> list=clazzServer.getAllClazz();
        return Result.success(list);
    }
}
